<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\PracticalTraining;
use App\Models\PracticalTrainingEvaluation;
use App\Models\PracticalTrainingUser;
use App\Services\PracticalTrainingService;
use App\Services\PracticalTrainingUserService;
use DateTime;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PracticalTrainingUserController extends AdminController
{
    //保存
    public function  save(Request $request){
        $validator = Validator::make($request->all(), [
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'practical_training_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'work_drawing' => 'required|string',
            'practical_result' => 'required|string',
            'content' => 'required|string'
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        /* 暂时不需要时间限制
        $trainingEndTime =  PracticalTrainingService::getInstance()->getByTrainingTime($request->practical_training_id);
        $ent_time = $trainingEndTime['ent_time'];
        $dateTime = new DateTime('now');
        $present_time =  $dateTime->format('Y-m-d H:i:s');
        if($present_time > $ent_time){
            return $this->fail(CodeResponse::AUTH_TRAINING_RESULT_REGISTERED);
        }
//        dd($dateTime->format('Y-m-d H:i:s'));
        */
        $putData = $request->except(['_method','_token','course_name']);
        $id = $request->input('id');
        $flight = PracticalTrainingUser::updateOrCreate(
            ['id' => $id],
            $putData
        );
         $usersId = $request->input('users_id');
        if($id){
             PracticalTrainingEvaluation::query()
                ->where('is_finally', 1)
                ->update([
                    'is_finally' => 0,
                ]);
                
            $evaluation = new PracticalTrainingEvaluation();
            $evaluation->practical_training_user_id = $id;
            $evaluation->audit = 0;
            $evaluation->users_id =$usersId;
            $evaluation->is_finally = 1;
            $evaluation->save();
        }
        return $this->success($flight->id);
    }

    //列表
    public function  userPracticalList(Request $request){
        $validator = Validator::make($request->all(), [
            'users_id' => 'integer|digits_between:1,20|min:1',
            'practical_training_id' => 'integer|digits_between:1,20|min:1',
            'name' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $users_id  = $request->input('users_id');
        $practical_training_id  = $request->input('practical_training_id');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');

        $list = PracticalTrainingUserService::getInstance()->userPracticalList($page,$limit,$sort,$order, $name,$users_id,$practical_training_id);
        return $this->success($list);
    }

    public function  getByPracticalTrainingUserInfo(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $id = $request->input('id');
        $list = PracticalTrainingUserService::getInstance()->getByPracticalTrainingUserInfo($id);
        return $this->success($list);
    }

    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $practicalTrainingUser = PracticalTrainingUser::find($request->id);
        $practicalTrainingUser->delete();
        return $this->success();
    }
    // 上传结果
    public function upfile(Request $request) {
        $url = config('filesystems.disks.practicalUserResult.url');
        $default = config('filesystems.disks.practicalUserResult.default');

        $file =  PracticalTrainingUserService::getInstance()->upfile($request,$url,$default,'practicalUserResult');
        return $this->success([
                'file'=>$file,
            ]
        );
    }
}
